package com.ms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ms.dao.VolunteerActivityMapper;
import com.ms.entity.VolunteerActivity;
import com.ms.service.inter.VolunteerActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 志愿活动服务实现类 - 实现志愿活动相关的业务逻辑
 * 继承ServiceImpl并实现VolunteerActivityService接口
 */
@Service
@Transactional
public class VolunteerActivityServiceImpl extends ServiceImpl<VolunteerActivityMapper, VolunteerActivity> implements VolunteerActivityService {

    @Autowired
    private VolunteerActivityMapper volunteerActivityMapper;

    /**
     * 保存志愿活动
     * @param volunteerActivity 志愿活动对象
     * @return 保存结果
     */
    @Override
    public boolean save(VolunteerActivity volunteerActivity) {
        try {
            int result = volunteerActivityMapper.insert(volunteerActivity);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("保存志愿活动失败: " + e.getMessage(), e);
        }
    }

    /**
     * 根据ID查找志愿活动
     * @param activityId 活动ID
     * @return 志愿活动对象
     */
    @Override
    public VolunteerActivity findById(Long activityId) {
        try {
            return volunteerActivityMapper.selectById(activityId);
        } catch (Exception e) {
            throw new RuntimeException("根据ID查找志愿活动失败: " + e.getMessage(), e);
        }
    }

    /**
     * 获取所有志愿活动
     * @return 志愿活动列表
     */
    @Override
    public List<VolunteerActivity> findAll() {
        try {
            return volunteerActivityMapper.selectList(null);
        } catch (Exception e) {
            throw new RuntimeException("获取所有志愿活动失败: " + e.getMessage(), e);
        }
    }

    /**
     * 更新志愿活动
     * @param volunteerActivity 志愿活动对象
     * @return 更新结果
     */
    @Override
    public boolean update(VolunteerActivity volunteerActivity) {
        try {
            int result = volunteerActivityMapper.updateById(volunteerActivity);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("更新志愿活动失败: " + e.getMessage(), e);
        }
    }

    /**
     * 删除志愿活动
     * @param activityId 活动ID
     * @return 删除结果
     */
    @Override
    public boolean delete(Long activityId) {
        try {
            int result = volunteerActivityMapper.deleteById(activityId);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("删除志愿活动失败: " + e.getMessage(), e);
        }
    }
    
    /**
     * 根据状态查找志愿活动列表
     * @param status 状态
     * @return 志愿活动列表
     */
    @Override
    public List<VolunteerActivity> findByStatus(Integer status) {
        try {
            QueryWrapper<VolunteerActivity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", status);
            return volunteerActivityMapper.selectList(queryWrapper);
        } catch (Exception e) {
            throw new RuntimeException("根据状态查找志愿活动失败: " + e.getMessage(), e);
        }
    }
    
    /**
     * 根据发布者ID查找志愿活动列表
     * @param publisherId 发布者ID
     * @return 志愿活动列表
     */
    @Override
    public List<VolunteerActivity> findByPublisherId(Long publisherId) {
        try {
            QueryWrapper<VolunteerActivity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("publisher_id", publisherId);
            return volunteerActivityMapper.selectList(queryWrapper);
        } catch (Exception e) {
            throw new RuntimeException("根据发布者ID查找志愿活动失败: " + e.getMessage(), e);
        }
    }
}